<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <meta http-equiv="X-UA-Compatible" content="ie=edge">
  <title>编辑文章</title>
  <link rel="stylesheet" href="./libs/bootstrap/css/bootstrap.min.css">
  <link rel="stylesheet" href="./css/reset.css">
  <link rel="stylesheet" href="./css/iconfont.css">
  <link rel="stylesheet" href="./css/main.css">
  <link rel="stylesheet" href="./libs/layui/css/layui.css">
  <!-- jq -->
  <script src="./libs/jquery/jquery.min.js"></script>
  <script src="./libs/layui/layui.js"></script>

</head>

<body>
  <div class="container-fluid">
    <div class="common_title">
      编辑文章
    </div>
    <div class="container-fluid common_con">
      <form class="form-horizontal article_form" id="form">
        <div class="form-group">
          <label for="inputTitle" class="col-sm-2 control-label">文章标题：</label>
          <div class="col-sm-10">
            <input type="text" name="title" class="form-control title" id="inputTitle" value="文章标题文字">
          </div>
        </div>
        <div class="form-group">
          <label for="inputCover" class="col-sm-2 control-label">文章封面：</label>
          <div class="col-sm-10">
            <img src="./images/2.jpg" class="article_cover">
            <input name="cover" type="file" id="inputCover" onchange="changeImg()">
          </div>
        </div>
        <div class="form-group">
          <label for="inputCategory" class="col-sm-2 control-label">文章类别:
          </label>
          <div class="col-sm-4">
            <select id="category" class="form-control category" name="categoryId">
              <option value=""></option>
            </select>
          </div>
        </div>
        <div class="form-group">
          <label class="col-sm-2 control-label">发布时间：</label>
          <div class="col-sm-4">
            <input id="articleDate" name='date' class="jeinput" type="text" placeholder="请选择发布日期" readonly>
          </div>
        </div>
        <div class="form-group">
          <label for="inputEmail3" class="col-sm-2 control-label">文章内容：</label>
          <div class="col-sm-10">
            <!-- 富文本编辑区域 -->
            <textarea id="articleContent">请输入文章内容</textarea>
          </div>
        </div>
        <div class="form-group">
          <div class="col-sm-offset-2 col-sm-10">
            <button type="submit" class="btn btn-success btn-edit" id="release">修改</button>
            <button type="submit" class="btn btn-default btn-draft" id="draft">存为草稿</button>
          </div>
        </div>
      </form>
    </div>
  </div>
</body>
<script src="./js/http.js"></script>
<script src="./libs/art-template/template-web.js"></script>
<script src="./libs/tinymce_5.5.1/tinymce.min.js"></script>
<script id="classificati" type="text/html">
  {{each data}}
  <option value="{{$value.id}}">{{$value.name}}</option>
  {{/each}}
</script>
<script>
  $(function () {
    res8();
    res9();
    res7();
    // name1();
    $('#release').click(function (e) {
      e.preventDefault();
      name4('已发布');
    });
    $('#draft').click(function (e) {
      e.preventDefault();
      name4('');
    });
  });
  // function name1() {

  //   $('#form').submit(function (e) {
  //     e.preventDefault();
  //     let information = $('#form').serialize();
  //     mylink('POST', '/admin/article/edit', information, function (resData) {
  //       alert(resData.msg);
  //     });
  //   });

  // }
  function changeImg() {
    let file = document.querySelector('#inputCover').files[0];
    let filepath = URL.createObjectURL(file);
    $('.article_cover').attr('src', filepath);
  }

  function name4(judge) {
    let id = name2();
    let title = $('#inputTitle').val();
    let cover = document.querySelector('#inputCover').files[0];
    let categoryId = $('#category').val();
    let date = $('#articleDate').val();
    let content = tinyMCE.editors['articleContent'].getContent();;
    let state = judge;

    let formData = new FormData();
    formData.append('id', id);
    formData.append('title', title);
    formData.append('cover', cover);
    formData.append('categoryId', categoryId);
    formData.append('date', date);
    formData.append('content', content);
    formData.append('state', state);
    mylink('POST', '/admin/article/edit', formData, function (resData) {
      window.location = './article_list.html';
    });
  }
  function res8() {
    layui.use('laydate', function () {
      var laydate = layui.laydate;
      //执行一个laydate实例
      laydate.render({
        elem: '#articleDate' //指定元素
      });
    });
  }
  function res9() {
    tinymce.init({
      selector: '#articleContent',
      language: 'zh_CN'
    });
  }
  function res7() {
    mylink('GET', '/admin/category/list', null, function (resData) {
      let ificat = template('classificati', resData);
      console.log(ificat);
      $("#category").append(ificat);
      name3();
    })
  }
  function name3() {
    let h_id = name2();
    mylink('get', '/admin/article/search', { id: h_id }, function (resData) {
      let data = resData.data
      $('#inputTitle').val(data.title);
      $('.article_cover').attr('src', data.cover);
      $('#category').val(data.categoryId);
      $('#articleDate').val(data.date);
      setTimeout(() => {
        tinyMCE.editors['articleContent'].setContent(data.content);
      }, 300);
    })
  }
  function name2() {
    let url = window.location.href;
    let arr = url.split('?');
    if (arr.length <= 1) {
      alert('格式错误');
      return;
    }
    let arr1 = arr[1];
    let arr2 = arr1.split('=');
    let arr3 = arr2[1];
    return arr3;
  }
</script>

</html>